package com.ruoyi.system.controller;

import com.mysql.cj.Session;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.Applications;
import com.ruoyi.system.domain.Companyinfo;
import com.ruoyi.system.service.IApplicationsService;
import com.ruoyi.system.service.ICompanyinfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.work.domain.Jobbasicinfo;
import com.ruoyi.work.service.IJobbasicinfoService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 投递记录Controller
 *
 * @author ruoyi
 * @date 2024-11-02
 */
@RestController
@RequestMapping("/system/applications")
public class ApplicationsController extends BaseController {
    @Autowired
    private IApplicationsService applicationsService;
    @Autowired
    private IJobbasicinfoService jobbasicinfoService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询投递记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:applications:list')")
    @GetMapping("/list")
    public TableDataInfo list(Applications applications) {
        List<Applications> list = applicationsService.selectApplicationsList(applications);
        for (Applications applications1 : list) {
            Jobbasicinfo jobbasicinfo = jobbasicinfoService.selectJobbasicinfoByJobId(applications1.getJobId());
            applications1.setJobTitle(jobbasicinfo.getJobTitle());
            applications1.setCompanyName(jobbasicinfo.getCompanyName());
            SysUser sysUser = iSysUserService.selectUserById(applications1.getUserId());
            applications1.setUserName(sysUser.getUserName());
        }
        return pagination(list);
    }

    /**
     * 导出投递记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:applications:export')")
    @Log(title = "投递记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Applications applications) {
        List<Applications> list = applicationsService.selectApplicationsList(applications);
        for (Applications applications1 : list) {
            Jobbasicinfo jobbasicinfo = jobbasicinfoService.selectJobbasicinfoByJobId(applications1.getJobId());
            applications1.setJobTitle(jobbasicinfo.getJobTitle());
            SysUser sysUser = iSysUserService.selectUserById(applications1.getUserId());
            applications1.setUserName(sysUser.getUserName());
            applications1.setCompanyName(jobbasicinfo.getCompanyName());
        }
        ExcelUtil<Applications> util = new ExcelUtil<Applications>(Applications.class);
        util.exportExcel(response, list, "投递记录数据");
    }

    /**
     * 获取投递记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:applications:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Applications applications = applicationsService.selectApplicationsById(id);
        Long userid = applications.getUserId();
        Long jobid = applications.getJobId();
        SysUser sysUser = iSysUserService.selectUserById(userid);
        applications.setUserName(sysUser.getUserName());
        Jobbasicinfo jobbasicinfo = jobbasicinfoService.selectJobbasicinfoByJobId(jobid);
        applications.setJobTitle(jobbasicinfo.getJobTitle());
        applications.setCompanyName(jobbasicinfo.getCompanyName());
        return success(applications);
    }

    /**
     * 新增投递记录
     */
    @PreAuthorize("@ss.hasPermi('system:applications:add')")
    @Log(title = "投递记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Applications applications) {
        SysUser sysUser = iSysUserService.selectUserByUserName(applications.getUserName());
        applications.setUserId(sysUser.getUserId());
        Jobbasicinfo jobbasicinfo = jobbasicinfoService.selectJobIdByJobNameandcompanyname(applications.getJobTitle(), applications.getCompanyName());
        applications.setJobId(jobbasicinfo.getJobId());
        return toAjax(applicationsService.insertApplications(applications));
    }

    /**
     * 修改投递记录
     */
    @PreAuthorize("@ss.hasPermi('system:applications:edit')")
    @Log(title = "投递记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Applications applications) {
        SysUser sysUser = iSysUserService.selectUserByUserName(applications.getUserName());
        applications.setUserId(sysUser.getUserId());
        Jobbasicinfo jobbasicinfo = jobbasicinfoService.selectJobIdByJobNameandcompanyname(applications.getJobTitle(), applications.getCompanyName());
        applications.setJobId(jobbasicinfo.getJobId());

        return toAjax(applicationsService.updateApplications(applications));
    }

    /**
     * 删除投递记录
     */
    @PreAuthorize("@ss.hasPermi('system:applications:remove')")
    @Log(title = "投递记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(applicationsService.deleteApplicationsByIds(ids));
    }

    /**
     * 文件上传
     */
    @Log(title = "文件上传", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:applications:upload')")
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam(name = "file",value = "file") MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            Applications applications=new Applications();
            applications.setFileurl(url);
            applicationsService.insertApplications(applications);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
